package nc.impl.mmpac.wr.pwr;

import nc.bs.mmpac.wr.bp.WrApproveBP;
import nc.bs.mmpac.wr.bp.WrBrBP;
import nc.bs.mmpac.wr.bp.WrDeleteBP;
import nc.bs.mmpac.wr.bp.WrInsertBP;
import nc.bs.mmpac.wr.bp.WrUnApproveBP;
import nc.bs.mmpac.wr.bp.WrUpdateBP;
import nc.bs.mmpac.wr.util.WrExceptionUtil;
import nc.bs.mmpac.wr.util.WrVOStatusUtil;
import nc.itf.mmpac.wr.pwr.IPwrMaintainService;
import nc.util.mmf.framework.gc.GCBillTransferTool;
import nc.vo.mmpac.putplan.entity.PutPlanVO;
import nc.vo.mmpac.wr.entity.AggWrVO;
import nc.vo.pub.BusinessException;
import nc.vo.pubapp.pattern.exception.ExceptionUtils;

public class PwrMaintainServiceImpl
  implements IPwrMaintainService
{
  public AggWrVO[] brForWr(PutPlanVO[] vos)
    throws BusinessException
  {
    try
    {
      WrBrBP bp = new WrBrBP();
      return bp.brForWr(vos);
    }
    catch (Exception e) {
      ExceptionUtils.marsh(e);
    }
    return null;
  }

  public AggWrVO[] audit(AggWrVO[] vos)
    throws BusinessException
  {
    try
    {
      GCBillTransferTool transTool = new GCBillTransferTool(vos);

      AggWrVO[] fullBills = (AggWrVO[])transTool.getClientFullInfoBill();

      AggWrVO[] originBills = (AggWrVO[])transTool.getOriginBills();
      WrApproveBP bp = new WrApproveBP();
      return bp.audit(fullBills, originBills);
    }
    catch (Exception e) {
      ExceptionUtils.marsh(e);
    }
    return null;
  }

  public AggWrVO[] unAudit(AggWrVO[] vos)
    throws BusinessException
  {
    try
    {
      new WrVOStatusUtil().setAggWrVOStatus(vos, 0);

      GCBillTransferTool transTool = new GCBillTransferTool(vos);

      AggWrVO[] fullBills = (AggWrVO[])transTool.getClientFullInfoBill();

      AggWrVO[] originBills = (AggWrVO[])transTool.getOriginBills();
      WrUnApproveBP bp = new WrUnApproveBP();

      return bp.unAudit(fullBills, originBills, 0);
    }
    catch (Exception e) {
      ExceptionUtils.marsh(e);
    }
    return null;
  }

  public AggWrVO[] insert(AggWrVO[] vos)
    throws BusinessException
  {
    try
    {
      return new WrInsertBP().insert(vos);
    }
    catch (Exception e) {
      ExceptionUtils.marsh(e);
    }
    return null;
  }

  public void delete(AggWrVO[] vos) throws BusinessException
  {
    try
    {
      GCBillTransferTool transferTool = new GCBillTransferTool(vos);

      AggWrVO[] fullBills = (AggWrVO[])transferTool.getClientFullInfoBill();
      WrDeleteBP bp = new WrDeleteBP();
      bp.delete(fullBills);
    }
    catch (Exception e) {
      WrExceptionUtil.marsh(e);
    }
  }

  public AggWrVO[] update(AggWrVO[] vos)
    throws BusinessException
  {
    GCBillTransferTool transTool = new GCBillTransferTool(vos);

    AggWrVO[] fullBills = (AggWrVO[])transTool.getClientFullInfoBill();

    AggWrVO[] originBills = (AggWrVO[])transTool.getOriginBills();
    try {
      WrUpdateBP bp = new WrUpdateBP();
      return bp.update(fullBills, originBills);
    }
    catch (Exception e) {
      WrExceptionUtil.marsh(e);
    }
    return null;
  }

  public AggWrVO[] insert_RequiresNew(AggWrVO[] vos)
    throws BusinessException
  {
    try
    {
      GCBillTransferTool transferTool = new GCBillTransferTool(vos);

      AggWrVO[] mergedVOs = (AggWrVO[])transferTool.getClientFullInfoBill();
      WrInsertBP bp = new WrInsertBP();
      return bp.insert(mergedVOs);
    }
    catch (Exception e) {
      ExceptionUtils.marsh(e);
    }
    return null;
  }

  public AggWrVO[] update_RequiresNew(AggWrVO[] vos)
    throws BusinessException
  {
    GCBillTransferTool transTool = new GCBillTransferTool(vos);

    AggWrVO[] fullBills = (AggWrVO[])transTool.getClientFullInfoBill();

    AggWrVO[] originBills = (AggWrVO[])transTool.getOriginBills();
    try {
      WrUpdateBP bp = new WrUpdateBP();
      return bp.update(fullBills, originBills);
    }
    catch (Exception e) {
      WrExceptionUtil.marsh(e);
    }
    return null;
  }

  public AggWrVO[] audit_RequiresNew(AggWrVO[] vos, boolean isAudit)
    throws BusinessException
  {
    try
    {
      new WrVOStatusUtil().setAggWrVOStatus(vos, 0);

      GCBillTransferTool transTool = new GCBillTransferTool(vos);

      AggWrVO[] fullBills = (AggWrVO[])transTool.getClientFullInfoBill();

      AggWrVO[] originBills = (AggWrVO[])transTool.getOriginBills();
      WrApproveBP bp = new WrApproveBP();
      return bp.audit(fullBills, originBills);
    }
    catch (Exception e) {
      ExceptionUtils.marsh(e);
    }
    return null;
  }
}